package com.wangsan.study.security;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.stereotype.Component;

@Component
public class FilterInvocationSecurityMetadataSourcePostProcessor implements BeanPostProcessor {
	private Logger logger = LoggerFactory.getLogger(FilterInvocationSecurityMetadataSourcePostProcessor.class);
	@Resource(name = "customFilterInvocationSecurityMetadataSource")
	private FilterInvocationSecurityMetadataSource securityMetadataSource;

	public Object postProcessAfterInitialization(Object bean, String name) {
		if (bean instanceof FilterSecurityInterceptor) {
			logger.info("filterInvocation security metadata source post processor,metadata source is {}",
				securityMetadataSource);
			((FilterSecurityInterceptor) bean).setSecurityMetadataSource(securityMetadataSource);
		}
		return bean;
	}

	public Object postProcessBeforeInitialization(Object bean, String name) {
		return bean;
	}
}
